package com.tanhua.admin.controller;

import com.tanhua.admin.service.SummaryService;
import com.tanhua.model.vo.SummaryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneOffset;
import java.util.HashMap;

@RestController
@RequestMapping("/dashboard")
public class DashboardController {

    @Autowired
    private SummaryService summaryService;

    @GetMapping("/summary")
    public ResponseEntity summary() {
        SummaryVo vo = summaryService.summary();
        return ResponseEntity.ok(vo);
    }

    @GetMapping("/users")
    public ResponseEntity users(Integer type, Long sd, Long ed) {
        //获取时间区间
        LocalDate start = Instant.ofEpochMilli(sd).atZone(ZoneOffset.ofHours(8)).toLocalDate();
        LocalDate end = Instant.ofEpochMilli(ed).atZone(ZoneOffset.ofHours(8)).toLocalDate();

        HashMap ret = new HashMap();

        //获取对应操作
        switch (type) {
            case 101://新增用户数量
                ret = summaryService.newUserCount(start, end);
                break;
            case 102://活跃用户数量
                ret = summaryService.activeUserCount(start, end);
                break;
            case 103://次日留存率
                ret = summaryService.retainedUserCount(start, end);
                break;
        }

        return ResponseEntity.ok(ret);
    }

    @GetMapping("/distribution")
    public ResponseEntity distribution(Long sd, Long ed) {
        //获取时间区间
        LocalDate start = Instant.ofEpochMilli(sd).atZone(ZoneOffset.ofHours(8)).toLocalDate();
        LocalDate end = Instant.ofEpochMilli(ed).atZone(ZoneOffset.ofHours(8)).toLocalDate();

        HashMap ret = summaryService.getDistribution(start, end);

        return ResponseEntity.ok(ret);
    }
}
